package com.bulemobi.activity.Ac_CarService;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.bulemobi.adapter.DriverAdapter;
import com.bulemobi.bean.AiDaiJiaBean;
import com.bulemobi.data.DataCentre;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.bulemobi.net.tsz.afinal.core.AsyncTask;
import com.bulemobi.urlCentre.UrlCentre;
import com.bulemobi.util.MD5Util;
import com.bulemobi.view.BaseActivity;
import com.bulemobi.view.titleView.ActionBarView;
import com.jsyh.hcb.R;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

/**
 * Created by 武 on 2014/9/23 0023.
 * 司机列表
 * item 已完成 ac_driver_item
 */
public class Ac_DriverList extends BaseActivity implements View.OnClickListener {
    @ViewInject(id = R.id.actionBar)
    ActionBarView actionBarView;
    @ViewInject(id = R.id.lv_driverList)
    ListView lv_driverList;
    @ViewInject(id = R.id.lay_detail)
    LinearLayout lay_detail;
    @ViewInject(id = R.id.tv_phoneNum)
    TextView tv_phoneNum;
    @ViewInject(id = R.id.tv_address)
    TextView tv_address;
    @ViewInject(id = R.id.tv_detail)
    TextView tv_detail;
    @ViewInject(id = R.id.tv_confirm)
    TextView tv_confirm;
    private DriverAdapter adapter;
    byte[] bytes;
    HttpClient client;
    String result;
    String stri;
    HttpPost httpPost;
    ArrayList<AiDaiJiaBean> driverList = Ac_ReplaceDriver.aiDaiJiaBeans;
    @ViewInject(id = R.id.lay_address)
    LinearLayout lay_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_driver_list_main);
        Intent intent = getIntent();
        int index = Integer.parseInt(intent.getStringExtra("index"));
        initData(index);
        initView();
    }

    private void initView() {
        lay_detail.setVisibility(View.VISIBLE);
        actionBarView.title.setText("司机列表");
        adapter = new DriverAdapter(this, driverList, "driver", getIMEI(this));
        lay_address.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //输入出发地点
            case R.id.lay_address:
                openActivity(Ac_InputStart.class);
                break;
            case R.id.tv_confirm:
                openActivity(Ac_Appointment.class);
                break;
        }
    }


    private void initData(int index) {
        tv_phoneNum.setText(driverList.get(index).getPhone());
        tv_detail.setText(driverList.get(index).getGoodrate());
        tv_address.setText(DataCentre.address);
        httpPost = new HttpPost(UrlCentre.ADJ_BASEURL);
        String la = driverList.get(index).getLat();
        String lo = driverList.get(index).getLng();
        String latLngMD5 = "";
        String name = driverList.get(index).getName();
        try {
//            TODO:这里手机号写死的
            latLngMD5 = MD5Util.encodeByMD5(driverList.get(index).getPhone() + "aidaijia");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        stri = "1212|" + name + "|" + driverList.get(index).getPhone() + "|莘凌路211号|" + driverList.get(index).getUcode() + "|" + la + "|" + lo + "|8|3fe5d0205970e8a732686c7cc2e74745|A00000389528060|" + latLngMD5 + "|remark";
        Log.d("Ac_replace", stri);
        bytes = new byte[0];
        client = new DefaultHttpClient();
        AsyncTask task = new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] params) {
                try {
                    bytes = stri.getBytes("UTF-8");
                    ByteArrayInputStream bs = new ByteArrayInputStream(bytes, 0, bytes.length);
                    HttpEntity entity = new InputStreamEntity(bs, bytes.length);
                    httpPost.setEntity(entity);
                    HttpResponse response = client.execute(httpPost);
                    result = EntityUtils.toString(response.getEntity(), "UTF-8");
                    Log.d("DOINBACKGROUND", result);
                    bs.close();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Object o) {
                Log.d("onPostExecute", result);
                DataCentre.orderNum = result;
            }
        };
        task.execute();
    }


}
